package com.example.swenp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class Freund_einladen extends Activity {

	FlipAnimation animation;
	EditText ed;
	ViewFlipper flipper;
	int gewaehlt;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.freund_einladen);

		animation = new FlipAnimation();
		gewaehlt = 0;
        flipper = (ViewFlipper) findViewById(R.id.freund_einladen_flipper);

        ed = (EditText)findViewById(R.id.ed_freund_einladen_freund_suchen);
        ed.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s) {
            	auflisten(ed.getText().toString());
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){}
        }); 
	}
	
	public void suche_loeschen(final View view){
    	ed.setText("");
    }
	
	public void auflisten(String _wen){
		String wen = _wen;
		LinearLayout ll = (LinearLayout) findViewById(R.id.freunde_platzhalter);
	  	LayoutInflater inflater = (LayoutInflater)this.getSystemService (Context.LAYOUT_INFLATER_SERVICE);

	  	ll.removeAllViews();
	  	
	  	if(!wen.equalsIgnoreCase("")){
	  	if(wen.substring(0,1).matches("m")){
	  		
		  		View vv = inflater.inflate(R.layout.kontaktleiste, null);
		  		ImageView img_bild = (ImageView) vv.findViewById(R.id.img_kontaktleiste_profilbild);
		  		TextView tx_benutzername = (TextView) vv.findViewById(R.id.tx_kontaktleiste_benutzername);
		  		TextView tx_status = (TextView) vv.findViewById(R.id.tx_kontaktleiste_status);
		  		TextView tx_online_offline = (TextView) vv.findViewById(R.id.tx_kontaktleiste_online_offline);
		  		ImageView img_online_offline = (ImageView) vv.findViewById(R.id.img_kontaktleiste_online_offline);
		  		
		  			img_bild.setImageResource(R.drawable.profilbild_rocky);
		  			tx_benutzername.setText("Max");
		  			tx_status.setText("");
		  			tx_online_offline.setText("online");
		  			img_online_offline.setImageResource(R.drawable.online);
		  		
		  		ll.addView(vv, new LinearLayout.LayoutParams(ll.getLayoutParams().width, ll.getLayoutParams().WRAP_CONTENT));
	  	}
	  	
	  	if(wen.substring(0,1).matches("p")){
	  		
	  		View vv = inflater.inflate(R.layout.kontaktleiste, null);
	  		ImageView img_bild = (ImageView) vv.findViewById(R.id.img_kontaktleiste_profilbild);
	  		TextView tx_benutzername = (TextView) vv.findViewById(R.id.tx_kontaktleiste_benutzername);
	  		TextView tx_status = (TextView) vv.findViewById(R.id.tx_kontaktleiste_status);
	  		TextView tx_online_offline = (TextView) vv.findViewById(R.id.tx_kontaktleiste_online_offline);
	  		ImageView img_online_offline = (ImageView) vv.findViewById(R.id.img_kontaktleiste_online_offline);
	  		
	  		img_bild.setImageResource(R.drawable.profilbild_justin);
  			tx_benutzername.setText("Peter");
  			tx_status.setText("");
  			tx_online_offline.setText("online");
  			img_online_offline.setImageResource(R.drawable.online);
  			
	  		ll.addView(vv, new LinearLayout.LayoutParams(ll.getLayoutParams().width, ll.getLayoutParams().WRAP_CONTENT));
  		}
	  	if(wen.substring(0,1).matches("h")){
	  		
	  		View vv = inflater.inflate(R.layout.kontaktleiste, null);
	  		ImageView img_bild = (ImageView) vv.findViewById(R.id.img_kontaktleiste_profilbild);
	  		TextView tx_benutzername = (TextView) vv.findViewById(R.id.tx_kontaktleiste_benutzername);
	  		TextView tx_status = (TextView) vv.findViewById(R.id.tx_kontaktleiste_status);
	  		TextView tx_online_offline = (TextView) vv.findViewById(R.id.tx_kontaktleiste_online_offline);
	  		ImageView img_online_offline = (ImageView) vv.findViewById(R.id.img_kontaktleiste_online_offline);
	  		
	  		img_bild.setImageResource(R.drawable.profilbild_therock);
  			tx_benutzername.setText("Hans Meier");
  			tx_status.setText("");
  			tx_online_offline.setText("offline");
  			img_online_offline.setImageResource(R.drawable.offline);
	  		
	  		ll.addView(vv, new LinearLayout.LayoutParams(ll.getLayoutParams().width, ll.getLayoutParams().WRAP_CONTENT));
  		}
	  	}
	}
	
	  @Override
		public void onBackPressed() {
	    	if(gewaehlt == 0){
	    		super.onBackPressed();	
	    	}
	    	if(gewaehlt == 1){
	    		sucheschliessen();
	    	}
		}
	
	 public void kontakte_suchen(final View view){
	    	flipper.setInAnimation(animation.inFromRightAnimation());
	     	flipper.setOutAnimation(animation.outToLeftAnimation());
	     	flipper.showNext();
			gewaehlt = 1;
		}
	    
	    public void suche_schliessen(final View view){
    		sucheschliessen();
	    }
	    
	    public void sucheschliessen(){
	    	flipper.setInAnimation(animation.inFromLeftAnimation());
	     	flipper.setOutAnimation(animation.outToRightAnimation());
	     	flipper.showPrevious();
			gewaehlt = 0; 
	    }
}
